package manager.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import manager.actionform.StudentForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import dao.StudentMagDao;
import dao.impl.StudentMagImpl;
import domain.Student;

/**
 * 
 * DispatchAction，管理员管理学生Action
 * 
 * May 10, 2009
 *
 * @author Fabian
 *
 */
public class ManagerStudentAction extends DispatchAction {

	/**
	 * 浏览所有学生的Action，但是要传递分页参数（start--开始，limit--间隔）
	 */
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentForm tf = (StudentForm)form;
		int start = tf.getStart();
		int limit = tf.getLimit();		
		
		StudentMagDao tmd = new StudentMagImpl();
		//获取此页面的学生
		List<Student> list = tmd.getPageStudent(start, limit);
		//获取总记录数
		int totalCount = tmd.getTotalCount();
		
		//组装JSON字符串，返回
		String resultArray = "";
		for (Iterator<Student> it = list.iterator(); it.hasNext(); ) {
			Student s = it.next();
			resultArray = resultArray + "{id: " + s.getId() + ", number: " + s.getNumber() +
							", name: '" + s.getName() + "', sex: '" + s.getSex() + "', password: '" +
							s.getPassword() + "', banji: '" + s.getClass_id() +  "', grade: '" + s.getGrade() + " 级', grade_id: " + 
							s.getGrade_id() + ", major: '" + s.getMajor() + "', major_id: " + s.getMajor_id() + "}";
										
			if (it.hasNext()) {
				resultArray = resultArray + ",";
			}			
		}
		
		
		String jsonStr = "";
		jsonStr = "{totalCount: " + totalCount + ", result: [ " + resultArray + " ]}";
		
		request.setAttribute("result", jsonStr);
				
		return mapping.findForward("show_result");
	}
	
	/**
	 * 删除学生，要传递学生的id
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentForm tf = (StudentForm)form;
		//获取删除传递到后台的id集合字符串
		String delIdStr = tf.getDelStr();
		
		//删除字符串中的id组的所有学生
		StudentMagDao tmd = new StudentMagImpl();
		boolean result = tmd.delStudents(delIdStr);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{result: 'success', msg: '恭喜，删除学生成功！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{result: 'failure', msg: '抱歉，删除学生失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}		
		
		return mapping.findForward("del_result");
	}
	
	/**
	 * 添加学生，要传递学生的信息
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		StudentForm tf = (StudentForm)form;		
		Student s = new Student();
		s.setId(tf.getId());
		s.setNumber(tf.getNumber());
		s.setPassword(tf.getPassword());
		s.setName(tf.getName());
		s.setSex(tf.getSex());
		s.setClass_id(tf.getClass_id());
		s.setGrade_id(tf.getGrade_id());
		s.setMajor_id(tf.getMajor_id());
		
		//添加新学生
		StudentMagDao tmd = new StudentMagImpl();
		boolean result = tmd.addStudent(s);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{success: true, msg: '恭喜，添加学生成功！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{failure: true, msg: '抱歉，添加学生失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}		
		return mapping.findForward("add_result");
	}
	
	/**
	 * 修改更新学生数据，要传递学生的id
	 */
	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StudentForm tf = (StudentForm)form;		
		Student s = new Student();
		s.setId(tf.getId());
		s.setNumber(tf.getNumber());
		s.setPassword(tf.getPassword());
		s.setName(tf.getName());
		s.setSex(tf.getSex());
		s.setClass_id(tf.getClass_id());
		s.setGrade_id(tf.getGrade_id());
		s.setMajor_id(tf.getMajor_id());
		
		//添加新学生
		StudentMagDao tmd = new StudentMagImpl();
		boolean result = tmd.modifyStudent(s);
		
		//组装返回的JSON数据
		String returnStr = "";
		if (result) {
			
			returnStr = "{success: true, msg: '恭喜，修改学生信息成功！'}";
			request.setAttribute("result", returnStr);
			
		} else {
			
			returnStr = "{failure: true, msg: '抱歉，修改学生信息失败！请重新尝试！'}";
			request.setAttribute("result", returnStr);
			
		}		
		return mapping.findForward("modify_result");
	}
}
